﻿/**
 * 
 * F. Mehmet ARSLAN (c) 2015
 * fmehmetarslan46@gmail.com
 *  
 **/
using ServiceState.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Caching;

namespace ServiceState.Helper
{
    public static class StateControl
    {
        public static System.Web.HttpContext httpContext { get; set; }

        public static ServiceStateThread serviceStateThread;
        public static SynchronizedCollection<Service> ServiceStateList
        {
            get;
            set;
        }

        public static ServicesState GetServicesState
        {
            get
            {
                XmlHelper xmlHelper = new XmlHelper();
                ServicesState serviceStateObj;
                Object cache = HttpRuntime.Cache["serviceStateObj"];
                if (cache == null)
                {
                    serviceStateThread.stop();
                    serviceStateObj = xmlHelper.ReadXml(httpContext.Server.MapPath("/App_Data/services.xml"));
                    HttpRuntime.Cache.Insert("serviceStateObj", serviceStateObj, new CacheDependency(httpContext.Server.MapPath("/App_Data/services.xml")), DateTime.Now.AddMinutes(5), TimeSpan.Zero);
                    refreshServicesState();
                   serviceStateThread.start();
                }
                else
                {
                    serviceStateObj = (ServicesState)cache;
                }
                return serviceStateObj;
            }
        }       

        private static void refreshServicesState(){
            ServicesState servicesState = GetServicesState;
            IList<Service> serviceStateList = new SynchronizedCollection<Service>();
            foreach (var serviceGroup in servicesState.ServiceGroup)
            {
                refreshStateServiceReqursive("", serviceGroup.Services, ref serviceStateList);
            }
            ServiceStateList = new SynchronizedCollection<Service>();
            foreach (var item in serviceStateList)
            {
                ServiceStateList.Add(item);
            }
        }
        private static void refreshStateServiceReqursive(String parentDescription, List<Service> services, ref IList<Service> serviceStateList)
        {
            if (!String.IsNullOrEmpty(parentDescription))
            {
                parentDescription += " > ";
            }
            foreach (var service in services)
            {
                if (!String.IsNullOrEmpty(service.Url))
                {
                    service.StatusCode = HttpStatusCode.SeeOther;
                    service.LongDescription = parentDescription + service.Description;
                    serviceStateList.Add(service);
                }
                if (service.ChildService.Any())
                {
                    refreshStateServiceReqursive(parentDescription+ service.Description, service.ChildService, ref serviceStateList);
                }
            }
        }


        public static HttpStatusCode fileControl(String url)
        {
            try
            {
                WebRequest request = WebRequest.Create(url);
                WebResponse response = request.GetResponse();
                String fileContent = "0";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream()))
                {
                    fileContent = sr.ReadToEnd();
                }
                if (fileContent.Equals("1"))
                    return HttpStatusCode.OK;
                else
                    return HttpStatusCode.NoContent;
            }
            catch (System.Net.WebException wexc)
            {
                if (wexc.Response == null)
                    return HttpStatusCode.InternalServerError;
                return ((HttpWebResponse)wexc.Response).StatusCode;
            }
        }

        public static HttpStatusCode httpControl(String url)
        {
            try
            {
                WebRequest request = WebRequest.Create(url);
                WebResponse response = request.GetResponse();
                return ((HttpWebResponse)response).StatusCode;
            }
            catch (System.Net.WebException wexc)
            {
                if (wexc.Response == null)
                    return HttpStatusCode.InternalServerError;
                return ((HttpWebResponse)wexc.Response).StatusCode;
            }
        }

    }
}